<?php 
   if($this->status == 1)
   {
?>

   <div id="box-content-middle">
    <div class="loading">Vui lòng đợi <img src="<?php echo $this->_baseUrl;?>/public/images/web/loading.gif"/></div>
    <?php 
    if($this->list != null)
    {
    	$count = count($this->list);
    	for ($i = 0; $i < $count; $i++) {
    		$id = $this->list[$i]['vm_id'];
    		$status = $this->list[$i]['status'];
    		
    ?>
        <div class = "vm-row <?php if($i % 2 == 0) echo " row0 "; else echo " row1 ";  echo "vm".$id?> ">
          <div style="width:35%;">
     		<p>Tên máy ảo: <?php echo $this->list[$i]['vmname'];?></p>
     		<p>Tên miền: <?php echo $this->list[$i]['domainname'];?> | IP:<?php echo $this->list[$i]['ip'];?> </p>
     		<p>Trạng thái: <span class="vm-row-status"> 
     		<?php 
     				if($status == 1) 
     		           echo '<font color="blue">Đang chạy</font>';
     		         else 
     		         	echo '<font color="red">Đã dừng</font>';
     		?>  </span></p>
     	</div> 
     	<div style="width:20%;">
     		<p>Gói : <?php echo $this->list[$i]['package_name'];?></p>
     		<p>RAM: <?php echo $this->list[$i]['ram'];?> MB | CPU: <?php echo $this->list[$i]['cpu'];?> nhân</p>
     		<p>Hệ điều hành: <?php echo $this->list[$i]['os_name'];?></p>
     	</div>
     	<div style="width:22%;">
     		<p>Ngày tạo :<?php echo date('d-m-Y',strtotime($this->list[$i]['created_date']));?></p>
     		<p>Tổng thời gian:
     		         <?php 
     		            $minuteDuration = intval($this->list[$i]['duration']);
     		            $h = intval($minuteDuration/60);
     		            $m = $minuteDuration % 60;
     		            if($h<10)
     		            	$h = '0'.$h;
     		            if($m<10)
     		            	$m = '0'.$m;
     		            echo $h.'h'.$m;
     		            
     		            $extra = intval($this->list[$i]['extratime']);
     		            if($extra > 0)
     		            {
	     		            $h = intval($extra/60);
	     		            $m = $extra % 60;
	     		            if($h<10)
	     		            	$h = '0'.$h;
	     		            if($m<10)
	     		            	$m = '0'.$m;
	     		            echo '+'.$h.'h'.$m;
     		            }
     		         ?>
     			  <a href="<?php echo $this->_baseUrl;?>/vm/upgrade/id/<?php echo $id;?>" title="Gia hạn.Khởi động lại máy ảo để thời gian gia hạn được sử dụng" style="vertical-align: text-top;">
                  <img src="<?php echo $this->_baseUrl;?>/public/images/web/plus.png" height="16px" width="16px"/>
                  </a>
     		</p>
     		<p>Đã sử dụng:<span class="vm-row-usedtime">
     		<?php 
     		            $usedtime = intval($this->list[$i]['usedtime']);
     		            $h = intval($usedtime/60);
     		            $m = $usedtime % 60;
     		            if($h<10)
     		            	$h = '0'.$h;
     		            if($m<10)
     		            	$m = '0'.$m;
     		            echo $h.'h'.$m;
     		            
     		 ?>
     		</span></p>
     	</div>
     	<div style="width:18%;">
     		<p>&nbsp;</p>
     		<p>Tắt máy
     		    <span class="vm-row-stop">
     		    <?php 
     		        if($status == 1)
     		        {
     		    ?>
     		    	<a href = "javascript:void(0);" onclick="clickVm(<?php echo $id;?>,0);" title="Tắt máy" style="vertical-align: text-top;">
                  	<img src="<?php echo $this->_baseUrl;?>/public/images/web/off.png" height="16px" width="16px"/>
                  	</a>
                <?php 
     		        }
     		        else 
     		        {
                ?>  
                    <a title="Tắt máy" style="vertical-align: text-top;">
                  	<img src="<?php echo $this->_baseUrl;?>/public/images/web/on_off.png" height="16px" width="16px"/>
                  	</a>
                <?php } ?>	
     		    </span>
     		</p>
     		<p>Bật máy
     		   <span class="vm-row-start">
     		    <?php 
     		        if($status == 0)
     		        {
     		    ?>
     		    	<a href = "javascript:void(0);" onclick="clickVm(<?php echo $id;?>,1);" title="Bật máy" style="vertical-align: text-top;">
                  	<img  src="<?php echo $this->_baseUrl;?>/public/images/web/on.png" height="16px" width="16px"/>
                  	</a>
                <?php 
     		        }
     		        else 
     		        {
                ?>  
                    <a title="Bật máy" style="vertical-align: text-top;">
                  	<img src="<?php echo $this->_baseUrl;?>/public/images/web/on_off.png" height="16px" width="16px"/>
                   	</a>
                <?php } ?>	
     		   		
     		   </span>
     		</p>
     	</div>
     </div>
    <?php		
    	}
    ?>
    
    <script type="text/javascript">
    	$(document).ready(function(){
         	 function showtime(time,vmname){
         		minute = time % 60;
         		if(minute<10)
             		minute = "0"+minute;
            	hour   = Math.floor(time/60);
            	if(hour<10)
                	hour = "0"+hour;
            	object = "."+vmname+" .vm-row-usedtime";
            	$(object).html(hour+"h"+minute);	
             }
             function display_stop(vmname,id){
                 obj_status = "."+vmname+" .vm-row-status";
                 obj_stop   = "."+vmname+" .vm-row-stop";
                 obj_start  = "."+vmname+" .vm-row-start";
                 $(obj_status).html('<font color="red">Đã dừng</font>');
                 $(obj_stop).html(
                    '<a title="Tắt máy" style="vertical-align: text-top;"><img src="<?php echo $this->_baseUrl;?>/public/images/web/on_off.png" height="16px" width="16px"/></a>'
                         );
                 $(obj_start).html(
                    '<a title="Bật máy" style="vertical-align: text-top;"><img src="<?php echo $this->_baseUrl;?>/public/images/web/on.png" height="16px" width="16px"/></a>'
                         );
             }

            <?php 
            
               for ($k = 0; $k < $count; $k++) {
               	  $id = $this->list[$k]['vm_id'];
    			  $status = $this->list[$k]['status'];
    			  if($status == 1)
    			  {
    			  	$duration = $this->list[$k]['duration'];
        	 	    $start = strtotime($this->list[$k]['start_date']);
        	        $used  = ceil(($this->now-$start)/60) + $this->list[$k]['usedtime'];
        	        if($used > $duration) $used = $duration;
    		?>	  
    				time<?php echo $id;?> = <?php echo $used;?>;
    				showtime(time<?php echo $id;?>,"vm<?php echo $id;?>");
    				interval<?php echo $id;?> = setInterval(function(){run<?php echo $id;?>();}, 60000);
    	            function run<?php echo $id;?>(){
    	           	      time<?php echo $id;?>++;
    				      showtime(time<?php echo $id;?>,"vm<?php echo $id;?>");
    				      if(time<?php echo $id;?> >= <?php echo $duration;?>){
    							clearInterval(interval<?php echo $id;?>);
    							display_stop("vm<?php echo $id;?>",<?php echo $id;?>);
    					  }
    	            }   	
    		<?php	
    			  }   
               }
            ?>
            
   		 });
    </script>
    
    <?php  
    }
    ?>
    
    </div>
    
    <div id="box-content-bottom">
   <?php 
   		loadHelper("pageslist");
   		pageslist($this->paginator);
   ?>
    </div>
<?php 
   }
   else 
   {
?>
    <div id="box-content-middle">
    </div>
     <div id="box-content-bottom">
     </div>
<?php 
   }
?>